package com.babai.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static final class PatternTypes {
        public static final String date = "yyyy-MM-dd";
        public static final String datetime = "yyyy-MM-dd HH:mm:ss";
        public static final String datetime_compact = "yyyyMMddHHmmss";
        public static final String month = "yyyy-MM";
    }

    public static Long longMilliSecond() {
        return now().getTime();
    }

    public static Date now() {
        return new Date();
    }

    public static Long longSecond() {
        return now().getTime() / 1000;
    }


    public static Date str2date(String source, String pattern) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            date = sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date str2date(String source) {
        return str2date(source, PatternTypes.date);
    }

    public static String date2str(Date date) {
        return date2str(date, PatternTypes.date);
    }

    public static String date2str(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String formatNow(String pattern) {
        return date2str(now(), pattern);
    }

    public static String getLastMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        return date2str(c.getTime(), PatternTypes.month);
    }

    public static String getYesterday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        return date2str(c.getTime(), PatternTypes.date);
    }

    public static String getDayBefore(int num) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 0 - num);
        return date2str(c.getTime(), PatternTypes.date);
    }

    public static String yyyymmdd() {
        return formatNow("yyyyMMdd");
    }

    public static String yyyymmdd(Date date) {
        return date2str(date, "yyyyMMdd");
    }

    public static String yyyyMMddHHmmssS(){
        return formatNow("yyyyMMddHHmmssS");
    }
    public static String HHmmss(){
        return formatNow("HHmmss");
    }

    public static void main(String[] args) {
        String a = "关于010098";
        int len = a.split("").length;
        System.out.println(a.substring(len - 6));
        System.out.println(a.substring(0, len - 6));

    }

    public static LocalDateTime getLocalDateTimeByYMD(String time){
        LocalDate localDate = LocalDate.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalTime localTime = LocalTime.parse("00:00:00", DateTimeFormatter.ofPattern("HH:mm:ss"));
        LocalDateTime dateTime = LocalDateTime.of(localDate, localTime);
        return dateTime;
    }

    public static LocalDateTime getLocalDateTimeByYMDHMS(String time){
        LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return localDateTime;
    }

}
